package com.zcf.shiro.web.controller;

import com.zcf.shiro.web.bean.Permissions;
import com.zcf.shiro.web.bean.Role;
import com.zcf.shiro.web.bean.User;
import explorer.web.api.ApiStatus;
import explorer.web.api.ApiStatusResult;
import explorer.web.rest.RestResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author zhaochaofeng
 * @date 2020/6/18 15:59
 */
@Controller
@RequestMapping("/user")
public class UserController extends CommonController {

    @ResponseBody
    @RequestMapping("/info")
    public RestResult info(){
        User user = getLoginUser();
        if(user==null)
        {
            return RestResult.createFailedResult("获取失败");
        }else{
            return RestResult.createSuccessResult(ApiStatus.OK.getCode(),user);
        }
    }

    @ResponseBody
    @RequestMapping("/permit")
    public RestResult permit(){
        User user = getLoginUser();
        Set<Permissions> permissions = new HashSet<>();;
        if(user==null)
        {
            return RestResult.createFailedResult("获取失败");
        }else{
            Set<Role> roles = user.getRoles();
            for(Role role : roles){
                permissions.addAll(role.getPermissions());
            }
            return RestResult.createSuccessResult(ApiStatus.OK.getCode(), permissions);
        }
    }

}
